<?php

if ( ! function_exists('set_new_filepath'))
{
	function set_new_filepath($filepath)
	{
        $path_parts = pathinfo($filepath);
        
        if ( ! is_array($path_parts))
            return FALSE;
            
        if ( ! (isset($path_parts['extension']) && !empty($path_parts['extension']) 
                    && isset($path_parts['dirname']) && !empty($path_parts['dirname'])))
            return FALSE;
        
        $path = $path_parts['dirname'] . '/'.'thumbs/';
        
        $file_ext = '.' . $path_parts['extension'];
        
		mt_srand();
        
		$filename = date('YmdHis').uniqid(mt_rand()).$file_ext;	            
	
		if ( ! file_exists($path.$filename))
		{
			return $path . $filename;
		}
	   
		$filename = str_replace($file_ext, '', $filename);
		
		$new_filename = '';
        
        if ( file_exists($path.$filename.$file_ext))
		{
			$new_filename = date('YmdHis').$filename.$file_ext;

		}
        
		if ($new_filename == '')
		{
			return FALSE;
		}
		else
		{
			return $path . $new_filename;
		}
	}
}

if ( ! function_exists('jcrop'))
{
    function jcrop()
    {   
        if ( !extension_loaded('gd') && !extension_loaded('gd2'))
        {
            trigger_error("GD is not loaded", E_USER_WARNING);
            return FALSE;
        }

        if ($_SERVER['REQUEST_METHOD'] == 'POST')
        {
            $x_axis = $_POST['x1'];            
            $y_axis = $_POST['y1'];
            
        	$targ_w = $_POST['x2'];
            $targ_h = $_POST['y2'];
            
            $w = $_POST['w'];
            $h = $_POST['h'];
            
        	$src = $_POST['filepath'];            
            
            if (($new_filename = set_new_filepath($src)) === FALSE)
                return FALSE;                
            
            $imgInfo = getimagesize($src);
            
            switch ($imgInfo[2])
            {
                case 1:
                    $im = imagecreatefromgif($src);
                    break;
                case 2:
                    $im = imagecreatefromjpeg($src);
                    break;
                case 3:
                    $im = imagecreatefrompng($src);
                    break;
                default:
                    trigger_error('Unsupported filetype!', E_USER_WARNING);
                    // break;
                    return FALSE;
            }

            $newImg = imagecreatetruecolor($targ_w, $targ_h);
            
            if (($imgInfo[2] == 1) || ($imgInfo[2] == 3))
            {
                imagealphablending($newImg, false);
                imagesavealpha($newImg, true);
                $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
                imagefilledrectangle($newImg, 0, 0, $targ_w, $targ_h, $transparent);
            }

            imagecopyresampled($newImg, $im, 0, 0, $x_axis, $y_axis, $targ_w, $targ_h, $w, $h);
            
            switch ($imgInfo[2])
            {
                case 1:
                    imagegif($newImg, $new_filename);
                    break;
                case 2:
                    imagejpeg($newImg, $new_filename);
                    break;
                case 3:
                    imagepng($newImg, $new_filename);
                    break;
                default:
                    trigger_error('Failed resize image!', E_USER_WARNING);
                    // break;
                    return FALSE;
            }                  
        }
        
        return $new_filename;
    }
}

if ( ! function_exists('manual_jcrop'))
{
    function manual_jcrop($x_axis, $y_axis, $targ_w, $targ_h, $w, $h, $src)
    {   
        if ( !extension_loaded('gd') && !extension_loaded('gd2'))
        {
            trigger_error("GD is not loaded", E_USER_WARNING);
            return FALSE;
        }           
        
        if (($new_filename = set_new_filepath($src)) === FALSE)
            return FALSE;                
        
        $imgInfo = getimagesize($src);
        
        switch ($imgInfo[2])
        {
            case 1:
                $im = imagecreatefromgif($src);
                break;
            case 2:
                $im = imagecreatefromjpeg($src);
                break;
            case 3:
                $im = imagecreatefrompng($src);
                break;
            default:
                trigger_error('Unsupported filetype!', E_USER_WARNING);
                 break;
                return FALSE;
        }

        $newImg = imagecreatetruecolor($targ_w, $targ_h);
        
        if (($imgInfo[2] == 1) || ($imgInfo[2] == 3))
        {
            imagealphablending($newImg, false);
            imagesavealpha($newImg, true);
            $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
            imagefilledrectangle($newImg, 0, 0, $targ_w, $targ_h, $transparent);
        }

        imagecopyresampled($newImg, $im, 0, 0, $x_axis, $y_axis, $targ_w, $targ_h, $w, $h);
        
        switch ($imgInfo[2])
        {
            case 1:
                imagegif($newImg, $new_filename);
                break;
            case 2:
                imagejpeg($newImg, $new_filename);
                break;
            case 3:
                imagepng($newImg, $new_filename);
                break;
            default:
                trigger_error('Failed resize image!', E_USER_WARNING);
                 break;
                return FALSE;
        }              
    
        return $new_filename;    
    }
}
?>